import json
from urllib import request
from urllib import parse
from urllib.request import Request

# # 1. 分析请求url
# data = {'name':'wert', 'age':23, 'addr':'henan'}
# url_encode = parse.urlencode(data)
# print(url_encode)
# url = f"http://httpbin.org/get?{url_encode}"
# # 2. 发起请求get
# response = request.urlopen(url)
# # 3. 解析数据
# if response.status == 200:
#     response_bytes = response.read()
#     response_str = response_bytes.decode("utf-8")
#     response_json = json.loads(response_str)
#     print(response_json['headers']['User-Agent'], response_json['args'])


# # 1. 发起请求get
# url = f"http://httpbin.org/get?name=wert"
# # 1.1. 构造请求实例
# req = Request(url, headers={
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0',
# }, method='GET')
# # 2. 发送请求
# response = request.urlopen(req)
# if response.status == 200:
#     response_bytes = response.read()
#     response_str = response_bytes.decode("utf-8")
#     response_json = json.loads(response_str)
#     print(response_json['headers']['User-Agent'], response_json['args'])



# # 1. 发起请求get
# url = f"http://httpbin.org/post?num=1"
# data = {'name':'wert', 'age':23, 'addr':'henan'}
# url_encode = parse.urlencode(data)
# print(url_encode)
# # 1.1. 构造请求实例
# req = Request(url, data=url_encode.encode() ,headers={
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0',
#     'content-type': 'application/x-www-form-urlencoded',
# }, method='POST')
# # 2. 发送请求
# response = request.urlopen(req)
# if response.status == 200:
#     response_bytes = response.read()
#     response_str = response_bytes.decode("utf-8")
#     response_json = json.loads(response_str)
#     print(response_json, response_json['headers']['User-Agent'])

# # 1. 发起请求get
# url = f"http://httpbin.org/post?num=1"
# data = {'name':'wert', 'age':23, 'addr':'henan'}
# json_data = json.dumps(data)
# print(json_data)
# # 1.1. 构造请求实例
# req = Request(url, data=json_data.encode() ,headers={
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0',
#     'content-type': 'application/json',
# }, method='POST')
# # 2. 发送请求
# response = request.urlopen(req)
# if response.status == 200:
#     response_bytes = response.read()
#     response_str = response_bytes.decode("utf-8")
#     response_json = json.loads(response_str)
#     print(response_json, response_json['headers']['User-Agent'])